/*
 * Ext GWT - Ext for GWT
 * Copyright(c) 2007-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
package web.mail.client;

import java.util.List;

import web.app.client.AppEvents;
import web.app.client.model.Folder;
import web.app.client.security.User;
import web.mail.client.model.MailItem;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Component;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

public class MailController extends Controller {

  private MailView mailView;
  private MailServiceAsync service;
  public static final String SERVICE = "mail.service";

  public MailController() {

	  registerEventTypes(MailEvents.FolderSelected);
	  registerEventTypes(MailEvents.FolderChanged);
	  registerEventTypes(MailEvents.FoldersUpdated);
	  
    /*
        registerEventTypes(MailEvents.NavMail);
        registerEventTypes(MailEvents.ViewMailItems);
        registerEventTypes(MailEvents.ViewMailItem);
    */

    service = (MailServiceAsync) GWT.create(MailService.class);
    ServiceDefTarget endpoint = (ServiceDefTarget) service;
    endpoint.setServiceEntryPoint( "/mail/service");
    Registry.register(SERVICE, service);

    Dispatcher.get().addController(this);
    
    mailView = new MailView(this);

    loadUserFolders(User.getCurrent().getUserId());
    
  }

  public void loadUserFolders( String userId ){
      service.getMailFolders(userId, new AsyncCallback<Folder>() {

        public void onSuccess(Folder result) {
            AppEvent ae = new AppEvent(MailEvents.FoldersUpdated, result);
            forwardToView(mailView, ae);
        }

        public void onFailure(Throwable caught) {
            Dispatcher.forwardEvent(AppEvents.Error, caught);
        }

    });
  }

  public Component getUI(){
      return mailView.viewport;
  }


  @Override public void handleEvent(AppEvent event) {

	  EventType type = event.getType();
	  if (type == MailEvents.FolderSelected) {
	      onFolderSelected( event );
	  }
	    
      /*
    EventType type = event.getType();
    if (type == MailEvents.NavMail) {
      forwardToView(mailView, event);
    } else if (type == MailEvents.ViewMailItems) {
      onViewMailItems(event);
    } else if (type == MailEvents.ViewMailItem) {
      forwardToView(mailView, event);
    }
    */
  }
  
  private void onFolderSelected( final AppEvent event ){
	  service.getMailItems((Folder) event.getData(), new AsyncCallback<List<MailItem>>() {

		@Override
		public void onFailure(Throwable caught) {
			Dispatcher.forwardEvent(AppEvents.Error, caught);
		}

		@Override
		public void onSuccess(List<MailItem> result) {
			AppEvent e = new AppEvent(MailEvents.FolderChanged);
			e.setData("data", result);
			e.setData("folder", event.getData());
	        forwardToView(mailView, e);
		}
	});
  }
/*
  private void onViewMailItems(final AppEvent event) {
    
    final Folder f = event.getData();
    if (f != null) {
      service.getMailItems(f, new AsyncCallback<List<MailItem>>() {

          @Override public void onSuccess(List<MailItem> result) {
          AppEvent ae = new AppEvent(event.getType(), result);
          ae.setData("folder", f);
          forwardToView(mailView, ae);
        }

        public void onFailure(Throwable caught) {
          Dispatcher.forwardEvent(AppEvents.Error, caught);
        }
      });
    }
  }
*/
}
